import env from '@/application/env/env.js'


const obj = {
	headerGET: {
		"Content-type": 'application/x-www-from-urlencoded'
	},
	headerPOST: {
		"Content-type": 'application/json',
		// 'token':uni.getStorageSync('token')?uni.getStorageSync('token'):''
	},
}
export const request = (options = {}) => {
	return new Promise((resolve, reject) => {
		const value = uni.getStorageSync('token')

		if (value && !options.url.includes('user/token')) {
			obj.headerPOST.Authorization = "Bearer " + value
		}
		uni.request({
			// url: apiBase + options.url,
			url: env.baseUrl + options.url,
			method: options.method,
			data: options.data,
			header: options.header ? options.header : obj.headerPOST,
			success: (response) => {
				console.log("request”",response.data)
				return resolve(response.data) //interceptorsRes(response.data, resolve)
			},
			fail: (fail) => {
				console.log("re--err”",fail)
			 return interceptorsErr(fail, reject);
			},
			complete: (c) => {
				// console.log(c, 22222)
			}
		})


	})
}
// 响应拦截
const interceptorsRes = (resp, resolve) => {

	resolve(resp)
}

// 异常处理
const interceptorsErr = (err, rejects) => {
	console.log("网络异常", err)
	// uni.showToast({
	// 	title: err.msg || '网络异常',
	// 	icon: 'none',
	// 	duration: 2000
	// })
	rejects(err)
}